package net.pws.oos.biz.query.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.pws.common.persistence.IPage;
import net.pws.common.persistence.QueryRepository;
import net.pws.oos.biz.model.Project;
import net.pws.oos.biz.query.ProjectFinder;
import net.pws.oos.biz.query.parameter.DataScope;
import net.pws.oos.biz.query.parameter.ProjectQueryParameter;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DefaultProjectFinder extends AbstractFinder implements
                                                        ProjectFinder {
    
    public int count(ProjectQueryParameter queryParameter) {
        List<Object> params = new ArrayList<Object>();
        StringBuilder hql = new StringBuilder(QueryRepository.getInstance()
                                                             .get("hql.countProject"));
        buildQueryRequest(queryParameter, hql, params);
        
        Number count = (Number) entityManager.findUnique(hql.toString(),
                                                         params.toArray());
        return count.intValue();
    }
    
    public List<Map> getProjectList(ProjectQueryParameter queryParameter,
                                    int start,
                                    int limit) {
        StringBuilder hql = new StringBuilder(QueryRepository.getInstance()
                                                             .get("hql.listProject"));
        List<Object> params = new ArrayList<Object>();
        buildQueryRequest(queryParameter, hql, params);
        hql.append(" order by p.year desc, p.name");
        IPage<Map> page = entityManager.findPage(hql.toString(),
                                                 params.toArray(),
                                                 start,
                                                 limit);
        
        return page.getData();
    }
    
    private void buildQueryRequest(ProjectQueryParameter queryParameter,
                                   StringBuilder hql,
                                   List<Object> params) {
        if (queryParameter == null) {
            return;
        }
        
        if (StringUtils.isNotEmpty(queryParameter.getName())) {
            hql.append(" and p.name like ? ");
            String like = "%" + queryParameter.getName() + "%";
            params.add(like);
        }
        if (StringUtils.isNotBlank(queryParameter.getOrganId())) {
            hql.append(" and p.organ.id=? ");
            params.add(queryParameter.getOrganId());
        }
        if (StringUtils.isNotBlank(queryParameter.getAreaId())) {
            hql.append(" and p.area.id=? ");
            params.add(queryParameter.getAreaId());
        }
        if (StringUtils.isNotBlank(queryParameter.getSpecialId())) {
            hql.append(" and p.special.id=? ");
            params.add(queryParameter.getSpecialId());
        }
        if (StringUtils.isNotBlank(queryParameter.getCustomerId())) {
            hql.append(" and p.customer.id=? ");
            params.add(queryParameter.getCustomerId());
        }
        
        if (queryParameter.getClose() != null) {
            hql.append(" and p.close=? ");
            params.add(queryParameter.getClose());
        }
        if (queryParameter.getYear() != null) {
            hql.append(" and p.year=? ");
            params.add(queryParameter.getYear());
        }
        
        if (queryParameter.getDataScope() == DataScope.Inside) {
            hql.append(" and p.sourceSystem is null and p.sourceId is null ");
        }
        else if (queryParameter.getDataScope() == DataScope.Outside) {
            hql.append(" and p.sourceSystem is not null and p.sourceId is not null ");
        }
        
    }
    
    // public boolean isProjectExists(String name, String code, String oldId) {
    // StringBuilder hql = new
    // StringBuilder("select count(p.id) from Project p where (p.name=? or p.code=?)");
    // List<Object> params = new ArrayList<Object>();
    // params.add(name);
    // params.add(code);
    // if (StringUtils.isNotEmpty(oldId)) {
    // hql.append(" and p.id<>? ");
    // params.add(oldId);
    // }
    // Number count = (Number) entityManager.findFirst(hql.toString(),
    // params.toArray());
    // return count.intValue() > 0;
    // }
    
    public Map getProject(String id) {
        StringBuilder hql = new StringBuilder(QueryRepository.getInstance()
                                                             .get("hql.listProject"));
        hql.append(" and p.id=? ");
        
        Map map = (Map) entityManager.findFirst(hql.toString(),
                                                new Object[] { id });
        return map;
    }
    
     public Project getProjectByName(String name) {
     StringBuilder hql = new StringBuilder("from Project p where p.name like ? ");
     
     List<Object> params = new ArrayList<Object>();
     params.add("%" + name + "%");

     Project p = (Project) entityManager.findFirst(hql.toString(), params.toArray());
     return p;
     }
}
